package com.mine.client.view.base;

import com.mine.client.view.exception.NotFoundException;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 单例
 * 加载基础映像图标 location（images/base/）
 *
 * @author jalasmart
 * 2024/3/21 9:41
 */
public class LoadingBaseImageIcon {
    private final static String DEFAULT_LOCATION = String.join("/", "images", "base"),
            ERROR_LOCATION = String.join("/", "images", "error");

    /**
     * 本地资源位置
     *
     * @author gongming
     * @date 2024/03/21
     */
    private final static class LocalURLs {
        static URL getBaseUrl() {
            URL url = LocalURLs.loadBaseResources(DEFAULT_LOCATION);
            if (url == null) {
                return LocalURLs.loadBaseResources(ERROR_LOCATION);
            }
            return url;
        }

        private static URL loadBaseResources(String locationName) {
            return LoadingBaseImageIcon.class.getClassLoader().getResource(locationName);
        }
    }

    public static String getBasePath() {
        URL baseUrl = LocalURLs.getBaseUrl();
        if (baseUrl == null) {
            throw new NotFoundException("获取基础资源路径异常:" + DEFAULT_LOCATION);
        }
        return baseUrl.getPath();
    }

    public final static Map<String, ImageIcon> CACHE_IMAGE_ICON = new ConcurrentHashMap<>();

    static {
        File fileDirectory = new File(getBasePath());
        loading(fileDirectory);
    }

    private static void loading(@NotNull File fileDirectory) {
        if (fileDirectory.isDirectory()) {
            loading(fileDirectory, fileDirectory.getName());
        }
    }

    private static void loading(@NotNull File fileDirectory, final String rootDirectory) {
        File[] listFiles = fileDirectory.listFiles();
        for (File file : Objects.requireNonNull(listFiles)) {
            if (file == null) {
                continue;
            }
            if (file.isDirectory()) {
                loading(file, rootDirectory);
            }
            if (file.isFile()) {
                putImageIcon(file, rootDirectory);
            }
        }
    }

    private static void putImageIcon(@NotNull File file, String rootDirectory) {
        String fileName = file.getName(), filePath = file.getPath(), suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 限制仅支持的后缀的类型
        if (".png.jpg".contains(suffixName)) {
            CACHE_IMAGE_ICON.put(uniqueCode(filePath.substring(filePath.indexOf(rootDirectory))),
                    new ImageIcon(file.getPath(), String.join("-", file.getParentFile().getName(), file.getName())));
        }
    }

    private static String uniqueCode(String relativeFilePath) {
        if (relativeFilePath == null) {
            return null;
        }
        return relativeFilePath.replaceAll("[/\\\\.]", "");
    }

    public static void main(String[] args) {
        System.out.println(LoadingBaseImageIcon.getBasePath());
        System.out.println(LoadingBaseImageIcon.CACHE_IMAGE_ICON.size());
        System.out.println(LoadingBaseImageIcon.CACHE_IMAGE_ICON.get(uniqueCode("base/a/close-2.png")).getImage());
    }
}
